﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyHelper;
using clf.weighing.entity;
namespace weighing
{
    /// <summary>
    /// 应用系统本地配制,登录窗口加载完成时加载配制 
    /// </summary>
    public class LocalConfig : BaseEntity
    {

        public static LocalConfig Default
        {
            get
            {
                return new LocalConfig
                {
                    Id =IdHelper.LongID,
                    ClientId = IdHelper.LongID,
                    SystemConfig = SystemConfig.Default,
                    ActiveCode = "",
                    RadomKey = "",
                    ActiveTime = null,
                    ImgConfig = ImgConfig.Default,
                    UserConfig = UserConfig.Default,
                    OtherConfig = OtherConfig.Default,
                    FunctionConfig = FunctionConfig.Default,
                    WeighingConfig = WeighingConfig.Default,
                    BillPrintConfig = BillPrintConfig.Default,                  
                    LedConfig = LedConfig.Default,    
                };
            }
        }

        /// <summary>
        /// 从数据库中 转换
        /// </summary>
        /// <param name="dbconfig">数据库中保存的配制</param>
        /// <returns></returns>
        public static LocalConfig FromDbConfig(Config dbconfig)
        {
            //加载所有配制
            LocalConfig config = new LocalConfig
            {
                Id = dbconfig.id,
                ClientId = (long)dbconfig.clientId,
                ActiveCode = dbconfig.activeCode,
                ActiveTime = dbconfig.activeTime,
                RadomKey = dbconfig.radomKey
            };

            SystemConfig systemConfig = (SystemConfig)JsonHelper.JsonToObject(dbconfig.systemConfig, typeof(SystemConfig));
            if (systemConfig == null)
            {
                systemConfig = SystemConfig.Default;
            }
            config.SystemConfig = systemConfig;

            UserConfig uc = JsonHelper.JsonToObject<UserConfig>(dbconfig.userConfig);
            config.UserConfig = uc ?? UserConfig.Default;

            OtherConfig oc = JsonHelper.JsonToObject<OtherConfig>(dbconfig.otherConfig);
            config.OtherConfig = oc ?? OtherConfig.Default;

            WeighingConfig wc = JsonHelper.JsonToObject<WeighingConfig>(dbconfig.weighingConfig);
            config.WeighingConfig = wc ?? WeighingConfig.Default;

            BillPrintConfig bpc = JsonHelper.JsonToObject<BillPrintConfig>(dbconfig.billPrintConfig);
            config.BillPrintConfig = bpc ?? BillPrintConfig.Default;


            FunctionConfig fc = JsonHelper.JsonToObject<FunctionConfig>(dbconfig.functionConfig);
            config.FunctionConfig = fc ?? FunctionConfig.Default;

            ImgConfig ic = string.IsNullOrEmpty(dbconfig.imgConfig)? ImgConfig.Default: JsonHelper.JsonToObject<ImgConfig>(dbconfig.imgConfig);
            ic.CapturePath = UrlHelper.Decode(ic.CapturePath);
            config.ImgConfig = ic ?? ImgConfig.Default;

       
            
            LedConfig ledConfig;
            if (dbconfig.ledConfig.IsNullOrEmpty())
            {
                ledConfig = LedConfig.Default;
            }
            else
            {
                ledConfig = JsonHelper.JsonToObject<LedConfig>(dbconfig.ledConfig);
            }
            config.LedConfig = ledConfig ?? LedConfig.Default;

            return config;
        }

        /// <summary>
        /// 客户端ID
        /// </summary>
        public long ClientId { get; set; }

        /// <summary>
        /// 其它 配制
        /// </summary>
        public OtherConfig OtherConfig { get; set; }
        /// <summary>
        /// 激活码
        /// </summary>
        public String ActiveCode { get; set; }  
        /// <summary>
        /// 随机码
        /// </summary>
        public String RadomKey { get; set; }
        /// <summary>
        /// 激活时间
        /// </summary>       
        public DateTime? ActiveTime { get; set; }
        /// <summary>
        /// 系统配制
        /// </summary>
        public SystemConfig SystemConfig { get; set; }

        /// <summary>
        /// 用户 配制
        /// </summary>
        public UserConfig UserConfig { get; set; }

        /// <summary>
        /// 过磅 配制
        /// </summary>
        public WeighingConfig WeighingConfig { get; set; }

        /// <summary>
        /// 功能控制 配制
        /// </summary>
        public FunctionConfig FunctionConfig { get; set; }

        /// <summary>
        /// 图片保存配制 配制
        /// </summary>
        public ImgConfig ImgConfig { get; set; }

        /// <summary>
        /// 磅单和打印 配制
        /// </summary>
        public BillPrintConfig BillPrintConfig { get; set; }

        public LedConfig LedConfig { get; set; }

    }
}
